# components/my-tooltip/my-tooltip.vue
<template>
	<view class="tooltip-wrapper">
		<!-- 触发元素 -->
		<view class="trigger" @tap.stop="toggleTooltip">
			<slot></slot>
		</view>

		<!-- 提示框 -->
		<block v-if="showTooltip">
			<view class="tooltip" :style="tooltipStyle">
				<view class="tooltip-arrow"></view>
				<view class="tooltip-content">
					<view @click="handleRemove">退出团队</view>
					<view v-if="isCreator" @click="handleDelete">解散团队</view>
				</view>
			</view>
			<!-- 遮罩层，用于点击外部关闭 -->
			<view class="mask" @tap.stop="hideTooltip"></view>
		</block>
	</view>
</template>

<script>
	import api from '../api/api'
	import TeamUsersVue from './TeamUsers.vue'
	import appp from '@/applogin.js'
	export default {
		props: {
			choosenTeam: {
				type: Object,
				default: null
			}
		},
		components: {
			TeamUsersVue
		},
		data() {
			return {
				isCreator: false,
				showTooltip: false,
				tooltipStyle: {},
				screenWidth: 375 // 默认值，会在created中更新
			}
		},

		created() {
			// 获取屏幕宽度
			const sysInfo = uni.getSystemInfoSync()
			this.screenWidth = sysInfo.windowWidth
		},
		mounted() {
			if (this.choosenTeam) {
				api.ifCreator({
					teamId: this.choosenTeam.teamId
				}).then(re => {
					this.isCreator = re.data.data
				})
			}
		},
		methods: {
			handleRemove() {
				if (this.isCreator) {
					this.$emit('creatorwannaout')
				} else {
					api.removeTeamRelationship({
						teamId: this.choosenTeam.teamId
					}).then(re => {
						appp.getTeam()
						uni.switchTab({
							url: '/pages/team_main/team_main'
						})
					})
				}
			},
			handleDelete() {
				api.deleteTeam({
					teamId: this.choosenTeam.teamId
				}).then(re => {
					appp.getTeam()
					uni.switchTab({
						url: '/pages/index/index'
					})
					uni.switchTab({
						url: '/pages/team_main/team_main'
					})
				})
			},
			toggleTooltip() {
				if (!this.showTooltip) {
					this.showTooltip = true
					this.$nextTick(() => {
						this.calculatePosition()
					})
				} else {
					this.hideTooltip()
				}
			},

			hideTooltip() {
				this.showTooltip = false
			},

			calculatePosition() {
				const query = uni.createSelectorQuery().in(this)
				query.select('.trigger').boundingClientRect(triggerRect => {
					// 如果没有获取到触发元素的位置信息，直接返回
					if (!triggerRect) return

					query.select('.tooltip').boundingClientRect(tooltipRect => {
						// 如果没有获取到tooltip的位置信息，直接返回
						if (!tooltipRect) return

						// 计算left位置，使tooltip不会超出右边界
						let left = triggerRect.left
						if (left + tooltipRect.width > this.screenWidth - 20) {
							// 如果会超出右边界，则将tooltip向左偏移
							left = this.screenWidth - tooltipRect.width - 20
						}

						// 设置tooltip位置
						this.tooltipStyle = {
							left: left + 'px',
							top: (triggerRect.bottom + 10) + 'px' // 显示在trigger下方10px处
						}
					}).exec()
				}).exec()
			}
		}
	}
</script>

<style>
	.tooltip-wrapper {
		position: relative;
		display: inline-block;
	}

	.tooltip-wrapper .trigger {
		display: inline-block;
	}

	.tooltip-wrapper .tooltip {
		position: fixed;
		z-index: 999;
		width: 300rpx;
		/* 固定宽度 */
	}

	.tooltip-wrapper .tooltip .tooltip-arrow {
		position: absolute;
		top: -12rpx;
		left: 20rpx;
		/* 调整箭头位置 */
		width: 0;
		height: 0;
		border: 12rpx solid transparent;
		border-bottom: 12rpx solid #FFFFFF;
		border-top: 0;
	}

	.tooltip-wrapper .tooltip .tooltip-content {
		background-color: #FFFFFF;
		box-shadow: 0 0 10rpx rgba(0, 0, 0, 0.1);
		border-radius: 8rpx;
		padding: 16rpx 24rpx;
		word-break: break-all;
		line-height: 1.5;
		/* Adds increased line spacing */
	}

	.tooltip-wrapper .tooltip .tooltip-title {
		font-size: 28rpx;
		font-weight: bold;
		color: #333333;
		margin-bottom: 8rpx;
	}

	.tooltip-wrapper .tooltip .tooltip-text {
		font-size: 26rpx;
		color: #666666;
	}

	.tooltip-wrapper .mask {
		position: fixed;
		top: 0;
		right: 0;
		bottom: 0;
		left: 0;
		z-index: 99;
	}
</style>